package com.gzhryc.shared_device.oem.micro.iot;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import org.apache.log4j.xml.DOMConfigurator;

import com.gzhryc.common.config.PropertyTools;
import com.gzhryc.common.event.QuartzServer;
import com.gzhryc.common.kafka.KafkaProducerFactory;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.common.redis.RedisTools;
import com.gzhryc.jdbc.ConnectionFactory;
import com.gzhryc.jdbc.models.ConnectInfo;
import com.gzhryc.shared_device.bxscn.code.BXSCNWorkCacheManager;
import com.gzhryc.shared_device.bxscn.code.services.PortableEnergyCabinetService;
import com.gzhryc.shared_device.cdb.code.CDBWorkCacheManager;
import com.gzhryc.shared_device.cdb.code.services.BatteryCabinetService;
import com.gzhryc.shared_device.cdcz.code.CDCZWorkCacheManager;
import com.gzhryc.shared_device.cdcz.code.services.ChargeSocketService;
import com.gzhryc.shared_device.common.BusyThreadPool;
import com.gzhryc.shared_device.oem.code.BaseConstants;
import com.gzhryc.shared_device.oem.code.PayServiceFactory;
import com.gzhryc.shared_device.oem.code.RedisDBConstants;
import com.gzhryc.shared_device.oem.micro.iot.jetty.JettyHttpServer;
import com.gzhryc.shared_device.oem.micro.iot.times.BXSCNOnlineCheckJob;
import com.gzhryc.shared_device.oem.micro.iot.times.CDBOnlineCheckJob;
import com.gzhryc.shared_device.oem.micro.iot.times.CDCZOnlineCheckJob;

public class Main {

	static Logger log = Logger.getLogger(Main.class);

	public static void main(String[] args) {
		try {
			String path = "config";
			if (args != null && args.length > 0) {
				path = args[0];
			}

			// 获取当前程序路径
			File logCfgFile = new File(path + "/log4j.xml");
			if (logCfgFile.exists()) {
				DOMConfigurator.configure(logCfgFile.getPath());
				log.info("加载" + path + "/log4j.xml日志配置信息");
			} else {
				DOMConfigurator.configure(Main.class.getClassLoader().getResource("log4j.xml"));
				log.info("加载默认log4j.xml日志配置信息");
			}

			File infoCfgFile = new File(path + "/info.properties");
			if (infoCfgFile.exists()) {
				PropertyTools.load(infoCfgFile);
				log.info("加载" + path + "/info.properties配置信息");
			} else {
				PropertyTools.load(Main.class.getClassLoader().getResourceAsStream("info.properties"));
				log.info("加载默认info.properties配置信息");
			}

			File redisCfgFile = new File(path + "/redis.properties");
			if (redisCfgFile.exists()) {
				PropertyTools tools = new PropertyTools(redisCfgFile);
				RedisTools.get().initial(tools.getProperties());
				log.info("加载" + path + "/redis.properties配置信息");
			} else {
				PropertyTools tools = new PropertyTools(Main.class.getClassLoader().getResourceAsStream("redis.properties"));
				RedisTools.get().initial(tools.getProperties());
				log.info("加载默认redis.properties配置信息");
			}

			ConnectInfo info = null;
			File iotLogsJdbcCfgFile = new File(path + "/jdbc.properties");
			if (iotLogsJdbcCfgFile.exists()) {
				Properties properties = new Properties();
				InputStream inStream = new FileInputStream(iotLogsJdbcCfgFile);
				BufferedReader bf = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
				properties.load(bf);
				info = new ConnectInfo(properties);
				ConnectionFactory.addDataSource(BaseConstants.JDBC_KEY, info);
				ConnectionFactory.setDefaultJdbcKey(BaseConstants.JDBC_KEY);
				log.info("加载" + path + "/jdbc.properties数据库连接配置信息");
			} else {
				Properties properties = new Properties();
				properties.load(Main.class.getClassLoader().getResourceAsStream("jdbc.properties"));
				info = new ConnectInfo(properties);
				ConnectionFactory.addDataSource(BaseConstants.JDBC_KEY, info);
				ConnectionFactory.setDefaultJdbcKey(BaseConstants.JDBC_KEY);
				log.info("加载默认jdbc.properties数据库连接配置信息");
			}

			IOTLogServer.self().start();	//启动IOT日志服务

			PayServiceFactory.self().initial(PropertyTools.get().getString("httpDomain"));

			if(BaseConstants.getCDB()){
				CDBWorkCacheManager.self().initial(RedisDBConstants.DEVICE_WORKING_CACHE_DB);
				BatteryCabinetService.self().offlineAll();		//充电宝

				RedisTools.get().clear(RedisDBConstants.CDB_DEVICE_DB);
				RedisTools.get().clear(RedisDBConstants.CDB_DEVICE_SLOT_DB);
			}

			if(BaseConstants.getCDCZ()){
				CDCZWorkCacheManager.self().initial(RedisDBConstants.DEVICE_WORKING_CACHE_DB);
				ChargeSocketService.self().offlineAll();		//充电插座

				RedisTools.get().clear(RedisDBConstants.CDCZ_DEVICE_DB);
				RedisTools.get().clear(RedisDBConstants.CDCZ_DEVICE_SLOT_DB);
			}

			if(BaseConstants.getBXSCN()){
				BXSCNWorkCacheManager.self().initial(RedisDBConstants.DEVICE_WORKING_CACHE_DB);
				PortableEnergyCabinetService.self().offlineAll();	//便携式储能电池

				RedisTools.get().clear(RedisDBConstants.BXSCN_DEVICE_DB);
				RedisTools.get().clear(RedisDBConstants.BXSCN_DEVICE_SLOT_DB);
			}

			//启动IOT服务
			IOTServerNodeFactory.self().initial();

			final String configPath = path;

			BusyThreadPool.self().initial(); //启动繁忙线程池，执行一些耗时任务
			BusyThreadPool.self().execute(new Runnable() {
				@Override
				public void run() {
					try {
						File kafkaCfgFile = new File(configPath + "/kafka_producer.properties");
						if (kafkaCfgFile.exists()) {
							PropertyTools propertyTools = new PropertyTools(kafkaCfgFile);
							KafkaProducerFactory.self().initial(propertyTools.getProperties());
							log.info("消息生产者加载" + configPath + "/kafka_producer.properties配置信息");
						} else {
							log.info("未加载kafka_producer.properties配置信息");
						}
					}catch (IOException e){
						log.error(e.getMessage(),e);
					}
				}
			});

			//定时执行
			QuartzServer.start();
			// 每5分钟
			QuartzServer.delayedRunJob("0 */5 * * * ?", CDCZOnlineCheckJob.class, false);
			QuartzServer.delayedRunJob("0 */5 * * * ?", CDBOnlineCheckJob.class, false);
			QuartzServer.delayedRunJob("0 */5 * * * ?", BXSCNOnlineCheckJob.class, false);

			JettyHttpServer.start();

		} catch (IOException e) {
			log.error(e.getMessage(),e);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
	}

}
